<template>
  <div >
    <Header></Header>
    <el-container>
      <el-row>
        <el-col :span="40 ">
          <el-card :body-style="{ padding: '0px' }">
            <img :src="film.pic" class="image">
            <div style="padding: 14px;">
              <span>{{film.name}}</span>
              <div class="bottom clearfix">
              <el-button type="text" class="button" @click="collection(film.id)">收藏</el-button>
              <el-button v-if="isVip" type="text" class="button" @click="goVideo(film.id)">在线播放</el-button>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-container>
    <el-collapse v-model="activeName" accordion>
      <el-collapse-item title="导演 Director" name="1">
        <div>{{film.director}}</div>
      </el-collapse-item>
      <el-collapse-item title="主演 Leading Actor" name="2">
       <div>{{film.actor}}</div>
      </el-collapse-item>
      <el-collapse-item title="影片介绍 Film Introduction" name="3">
        <div>{{film.introduction}}</div>
      </el-collapse-item>
      <el-collapse-item title="评分 Rate" name="4">
        <div v-if="rate">
          <el-rate @change="setRate(value)"
              v-model="value"
              show-text >
          </el-rate>
        </div>
        <div v-else>
          <el-rate
              v-model="result"
              disabled
              show-score
              text-color="#ff9900"
              :score-template="result">
          </el-rate>
      </div>

      </el-collapse-item>
      <el-collapse-item title="评论 comment" name="5">
        <el-input
            placeholder="输入关键字进行过滤"
            v-model="filterText">
        </el-input>

        <el-tree
            class="filter-tree"
            :data="comments"
            :props="defaultProps"
            default-expand-all
            :filter-node-method="filterNode"
            ref="tree">
        </el-tree>
       <el-button type="text" @click="dialogFormVisible = true">评论</el-button>

        <el-dialog title="我的评论" :visible.sync="dialogFormVisible">
          <el-form :model="form">
            <el-form-item label="评论内容" :label-width="formLabelWidth" prop="content">
              <el-input v-model="form.content" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitForm('form')">确 定</el-button>
          </div>
        </el-dialog>
      </el-collapse-item>
    </el-collapse>


  </div>
</template>

<script>
import Header from "../components/Header";
export default {
  name: "Film.vue",
  components: {Header},
  data() {
    return {
      film:'',
      user:this.$store.getters.getUser,
      isVip:this.$store.getters.getUser.isVip,
      value: null,
      activeName: '1',
      rate:true,
      result: '',
      filterText: '',
      defaultProps: {
        children: 'children',
        label: 'content'
      },
      comments:{},
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        createdUserId:this.$store.getters.getUser.id,
        content: '',
      },
      scoreForm:{
        grade:'',
        filmId:'',
        createdUserId: this.$store.getters.getUser.id
      },
      formLabelWidth: '120px',
      collectionForm:{
        filmId:'',
        CreatedUserId:''
      }
    }
  },
  methods: {
    goVideo(id){
      this.$router.push("/video/"+id)
    },
    setRate(value){
      debugger;
      //异步请求提交评分
      this.scoreForm.grade=value
      this.scoreForm.filmId=this.film.id
      this.$axios.post("/score/add/",this.scoreForm).then(res =>{
          if(res.data.data===true){
            this.$message({
              showClose: true,
              message: '评分成功',
              type: 'success'
            });
            this.rate=false
          }else{
            this.$message.error("网络繁忙，评分失败！");
          }
      })
    },
    filterNode(value, data) {
      debugger;
      if (!value) return true;
      return data.content.indexOf(value) !== -1;
    },
    submitForm(formName) {
          const _this=this
          debugger;
          this.form.filmId=this.film.id
          this.$axios.post("/comment/add",this.form).then(res =>{

            if(res.data.code===200){
              this.$message({
                showClose: true,
                message: '评论成功',
                type: 'success'
              });
             this.dialogFormVisible=false
              this.$axios.post('/comment/film/' + this.film.id).then(res => {
                const comments = res.data.data
                this.comments=comments
              })
            }else{
              this.$message.error(res.data.message);
            }
      });
    },
    collection(id){
      const _this = this
      this.collectionForm.filmId=id
      this.collectionForm.createdUserId=this.$store.getters.getUser.id
      _this.$axios.post("/collection/add",this.collectionForm).then(res => {
        if(res.data.data===true){
          this.$message({
            showClose: true,
            message: '收藏成功',
            type: 'success'
          });
        }else{
          this.$message.error("收藏失败");
        }
      })
    }
  },
  created() {
    //拿到电影id
    const id = this.$route.params.id
    const userId=this.user.id
    //通过异步请求查询电影信息
    if(id) {
      this.$axios.get('/film/get/' + id).then(res => {
        const film = res.data.data
        this.film=film
      }),
          //获取电影平均分
      this.$axios.get('/score/film/' + id).then(res => {
        const score = res.data.data
        this.result=score

      }),
     this.$axios.post('/comment/film/' + id).then(res => {
        const comments = res.data.data
        this.comments=comments
     })
      }
    if(userId){
      //查询是否有自己的评分 如果该电影已经评分 则直接显示最终分数
      this.$axios.post('/score/' + id+'/'+userId).then(res => {
        debugger
        //已经评过分的情况
          if(res.data.data===true){
            this.rate=false
          }else{
            this.rate=true
          }
      })
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },

};
</script>
<style>
.time {
  font-size: 13px;
  color: #999;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
}

.button {
  padding: 0;
  float: right;
}

.image {
  width: 100%;
  display: block;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}
</style>
